$(function () {
    $("#backPage").load("/html/backPage.html");
})

let codeUser = sessionStorage.getItem("userCode");

// 查询所有单词
function findAll() {
    let data = {
        key: $("#selectKey").val(),
        startTime: $("#startTime").val(),
        endTime: $("#endTime").val(),
        pageNum: $("#pageNum").val(),
        pageSize: $("#pageSize").val(),
        userCode: codeUser
    };
    let result = jsonGetValue("/w/word/getSelectList", data);
    console.log(result);
    setAllData(result.data.list);
    $("#total").html(result.data.count);
}
// 向单词列表中填充数据
function setAllData(data) {
    let html = "";
    for (let i=0; i<data.length;i++) {
        html += "<tr>\n" +
            '        <td><input type="checkbox" name="checkOne" value="' + data[i].id + '"></td>\n' +
            "        <td>" + data[i].id + '</td>\n' +
            "        <td>" + data[i].word+ "</td>\n" +
            "        <td>" + data[i].translate + "</td>\n" +
            "        <td>" + data[i].name + "</td>\n" +
            "        <td id='likeNum'>" + data[i].likeCount + "</td>\n" +
            "        <td>" + data[i].createTime + "</td>\n";
        html += "    <td>" +
            "            <div onclick=\"doLike('"+ data[i].id +"','"+ data[i].userCode +"')\" title='点赞'>" +
            "                <i class='iconfont icon-like' id='like" + data[i].id +"' style='color:" + getLikeColor(data[i].isLike) + "'>&#xe644;</i>" +
            "            </div>\n" +
            "            <div onclick=\"doCollect('"+ data[i].id +"')\" title='收藏'>" +
            "                <i class='iconfont icon-collect' id='collect" + data[i].id +"' style='color:" + getCollectColor(data[i].isCollect) + "'>&#xe732;</i>" +
            "            </div>\n" +
            "        </td>" +
            "    </tr>";
    }
    $("#tbody").html(html);
    $("#tbody > tr > td").css({"vertical-align":"middle"})
    $("#tbody > tr > td:last-child").css({"display": "flex","justify-content": "flex-start"})
    $("#tbody > tr > td:last-child > div").css({"cursor": "pointer"})
    $("#tbody > tr > td:last-child > div").css({"margin-right":"10px"})
    myCheckAO();
}

// 判断是否点赞
function getLikeColor(isLike) {
    if (isLike == "liked"){
        return "rgb(255,113,60)";
    } else {
        return "rgb(55,175,250)"
    }
}
// 判断是否收藏
function getCollectColor(isCollect) {
    if (isCollect == "collected"){
        return "rgb(255,77,56)";
    } else {
        return "rgb(153,155,156)"
    }
}
// 提交单词
function addWord() {
    let word =  $("#addWord").val();
    let translate = $("#addTranslate").val();
    let userCode = codeUser;
    let data = {
        word:word,
        translate: translate,
        userCode: userCode
    };
    let result = jsonPost("/w/word/addWord", data);
    console.log(result);
    if (result.data.result == "success") {
        $(".right").load("/html/word/word-list.html");
    }
}

// 点赞/取消点赞
function doLike(id,likedUser){
    let data = {
        wordId:id,
        userCode: userCode,
        likedUser: likedUser
    };
    let judge = jsonGetValue("/w/word/getLikeByData", data);
    console.log(judge);
    if (judge.data.data == null){
        let result = jsonPost("/w/word/addLike", data);
        console.log(result);
        if (result.data.data == "success") {
            $("#like" + id).css({"color":"rgb(255,113,60)"});
            page('go');
        }
    } else {
        let param = {
            wordId:id,
            userCode: userCode,
        };
        if (judge.data.data.stateCode == "liked"){
            Object.assign(param, {stateCode: "unliked"});// js对象的合并
            $("#like" + id).css({"color":"rgb(55,175,250)"});
        } else {
            Object.assign(param, {stateCode: "liked"});// js对象的合并
            $("#like" + id).css({"color":"rgb(255,113,60)"});
        }
        let result = jsonPost("/w/word/updateLike", param);
        console.log(result);
        if (result.data.data == 1) {
            page('go');
        }
    }
}

// 收藏/取消收藏
function doCollect(id) {
    let data = {
        wordId:id,
        userCode: codeUser
    };
    let judge = jsonGetValue("/w/word/getCollectByData", data);
    console.log(judge);
    if (judge.data.data == null){
        let result = jsonPost("/w/word/addCollect", data);
        console.log(result);
        if (result.data.data == "success") {
            page('go');
            $("#collect" + id).css({"color":"rgb(255,77,56)"});
        }
    } else {
        let param = {
            wordId:id,
            userCode: codeUser
        };
        if (judge.data.data.stateCode == "collected"){
            Object.assign(param, {stateCode: "uncollected"});// js对象的合并
            $("#collect" + id).css({"color":"rgb(153,155,156)"});
        } else {
            Object.assign(param, {stateCode: "collected"});// js对象的合并
            $("#collect" + id).css({"color":"rgb(255,77,56)"});
        }
        let result = jsonPost("/w/word/updateCollect", param);
        console.log(result);
        if (result.data.data == 1) {
            page('go');
        }
    }
}

// 获取多选框选择的单词
function getCheckWord() {
    let words = [];
    $("[name='checkOne']:checkbox").each(function(i){
        if ($(this).prop("checked") == true){
            words.push(parseInt($(this).val()));
        }
    })
    words = JSON.stringify(words);
    words = words.replace("[","").replace("]","");
    return words;
}

// 批量导出文档
function createDeletion() {
    let words = getCheckWord();
    let data = {ids:words}
    let result = jsonPostParam("/w/word/createDeletion", data);
    console.log(result);
    if (result.data == "ok"){
        $(".right").load("/html/word/word-list.html");
    }
}

// 查询点击事件
function select() {
    page("home");
}
// 清空点击事件
function clearAll() {
    $("input[ type='text']").val("");
    $("input[ type='date']").val("");
    $("#pageSize").val(10);
    $("#pageNum").val(1);
}